#include "MoveCharacter.h"


cMoveCharacter::cMoveCharacter(cInfoUnion *InfoUnion,cMyCharacter* character,
							   list<cCharMovePosUnit> PosUnion,int aimX,int aimY)
{
	this->s_Character = character;
	this->s_InfoUnion = InfoUnion;
	this->m_PosUnion = PosUnion;

	this->m_TimeCounter = 0;

	this->m_AimMatrixX = aimX;
	this->m_AimMatrixY = aimY;

	this->s_BackFocus = NULL;

	//this->m_PosUnit = PosUnion.front();
	//this->m_PosUnion.pop_front();
}

cMoveCharacter::~cMoveCharacter()
{
	this->s_Character = NULL;
}

BOOL cMoveCharacter::Execute(unsigned char *keystate, BOOL *locks, int ex )
{
	if (this->m_PosUnion.empty() == true )
	{
		return MY_CHARACTER_MOVING_DONE;
	}
	
	if ( timeGetTime()-m_TimeCounter < s_InfoUnion->CharacterMoveTimeMate )
	{
		return MY_CHARACTER_MOVING;
	}

	float TargetX = m_PosUnion.front().X * s_InfoUnion->MatrixUnitWidth;
	float TargetY = m_PosUnion.front().Y * s_InfoUnion->MatrixUnitHeight;

	m_TimeCounter = timeGetTime();

	if (s_Character->GetSysPosX() > TargetX)
	{
		s_Character->SetCharDir(CHAR_DIR_Left);
		s_Character->SetSysPosX( s_Character->GetSysPosX() - s_InfoUnion->CharacterStepDistance );

		if ( s_Character->GetSysPosX() < TargetX )
		{
			s_Character->SetSysPosX( TargetX );
		}
	}

	if (s_Character->GetSysPosX() < TargetX)
	{
		s_Character->SetCharDir(CHAR_DIR_Right);
		s_Character->SetSysPosX( s_Character->GetSysPosX() + s_InfoUnion->CharacterStepDistance );

		if ( s_Character->GetSysPosX() > TargetX )
		{
			s_Character->SetSysPosX( TargetX );
		}
	}

	if (s_Character->GetSysPosY() > TargetY)
	{
		s_Character->SetCharDir(CHAR_DIR_Up);
		s_Character->SetSysPosY( s_Character->GetSysPosY() - s_InfoUnion->CharacterStepDistance );

		if ( s_Character->GetSysPosY() < TargetY )
		{
			s_Character->SetSysPosY( TargetY );
		}
	}

	if (s_Character->GetSysPosY() < TargetY)
	{
		s_Character->SetCharDir(CHAR_DIR_Down);
		s_Character->SetSysPosY( s_Character->GetSysPosY() + s_InfoUnion->CharacterStepDistance );

		if ( s_Character->GetSysPosY() > TargetY )
		{
			s_Character->SetSysPosY( TargetY );
		}
	}

	if ( s_Character->GetSysPosX() == TargetX 
		&& s_Character->GetSysPosY() == TargetY )
	{
		this->m_PosUnion.pop_front();
	}


	return MY_CHARACTER_MOVING;
}

BOOL cMoveCharacter::ReceiveDirective(int message)
{
	return TRUE;
}

BOOL cMoveCharacter::SetCharacter(cMyCharacter *character)
{
	return TRUE;
}

cMyCharacter *cMoveCharacter::GetCharacter()
{
	return this->s_Character;
}

void cMoveCharacter::SetBackFocus(cMyFocus *focus)
{
	this->s_BackFocus = focus;
}

cMyFocus *cMoveCharacter::GetBackFocus()
{
	return s_BackFocus;
}

BOOL cMoveCharacter::SetAimMatrixX(int aimX)
{
	this->m_AimMatrixX = aimX;
	return TRUE;
}

int cMoveCharacter::GetAimMatrixX()
{
	return this->m_AimMatrixX;
}

BOOL cMoveCharacter::SetAimMatrixY(int aimY)
{
	this->m_AimMatrixY = aimY;
	return TRUE;
}

int cMoveCharacter::GetAimMatrixY()
{
	return this->m_AimMatrixY;
}

cCharMovePosUnit::cCharMovePosUnit(float  x, float  y)
{
	this->X = x;
	this->Y = y;
}

cCharMovePosUnit::cCharMovePosUnit()
{
	this->X = -1;
	this->Y = -1;
}